package com.tz.web;


import com.tz.util.weixinUtils.pay.PayCommonUtil;
import net.sf.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;


@Controller
public class WeiXinController {

    String timeMillis = String.valueOf(System.currentTimeMillis() / 1000);
    String randomString = PayCommonUtil.getRandomString(32);

    /**
     * 调用微信支付服务器端接口
     *
     * @param sn          订单号
     * @param totalAmount 支付金额
     * @param description 产品描述
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/weixin/weixinPay/{sn}/{totalAmount}/{description}/{openId}/{orderType}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public SortedMap<String, Object> toWeiXinPay(@PathVariable String sn, @PathVariable BigDecimal totalAmount, @PathVariable String description, @PathVariable String openId, @PathVariable String orderType, HttpServletRequest request) {
        Map<String, String> map = PayCommonUtil.weixinPrePay(sn, totalAmount, description, openId, randomString, orderType, request);
        SortedMap<String, Object> finalpackage = new TreeMap<String, Object>();//通过子类TreeMap实例化接口对象 可用于排序
        finalpackage.put("appId", PayCommonUtil.APPID);
        finalpackage.put("timeStamp", timeMillis);
        finalpackage.put("nonceStr", randomString);
        finalpackage.put("package", "prepay_id=" + map.get("prepay_id"));
        finalpackage.put("signType", "MD5");
        String sign = PayCommonUtil.createSign("UTF-8", finalpackage);
        finalpackage.put("paySign", sign);
        System.out.println("进入支付方法了吗.........");
        return finalpackage;
    }


    /**
     * 企业付款业务是基于微信支付商户平台的资金管理能力，为了协助商户方便地实现企业向个人付款，针对部分有开发能力的商户，提供通过API完成企业付款的功能。
     * 比如目前的保险行业向客户退保、给付、理赔
     *
     * @param sn          订单号
     * @param amount      金额
     * @param description 企业付款操作说明信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/weixin/enterprisePayment/{sn}/{amount}/{description}/{openId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> enterprisePayment(@PathVariable String sn, @PathVariable BigDecimal amount, @PathVariable String description, @PathVariable String openId, HttpServletRequest request) {
        Map<String, String> map = PayCommonUtil.weixinEnterprisePayment(sn, amount, description, openId, request);
        System.out.println("企业向个人付款===========" + JSONObject.fromObject(map));
        return null;
    }

    /**
     * 查询企业对个人付款
     */
    @ResponseBody
    @RequestMapping(value = "/weixin/queryWeixinEnterprisePayment/{sn}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> queryWeixinEnterprisePayment(@PathVariable String sn) {
        Map<String, String> map = PayCommonUtil.queryWeixinEnterprisePayment(sn);
        System.out.println("查询企业向个人付款===========" + JSONObject.fromObject(map));
        return null;
    }
}